<?php

namespace app\admin\model\commission;

use app\admin\model\Order;
use app\admin\model\User;
use think\Model;
use traits\model\SoftDelete;

class Log extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'commission_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'user_name',
        'order_user_name',
        'order_no',
        'role_text'
    ];

    public $rolelist=[
        "platform"=>"平台",
        "agent"=>"代理商",
        "room"=>"星伴室",
        "parent"=>"直推",
        "parent2"=>"间推",
        "companion"=>"陪伴师",
        ];

    /*添加用户佣金记录*/
    public function addLog($data){

        $data=[
            "user_id"=>$data['user_id'],//获得者ID
            "order_id"=>$data['order_id'],//订单ID
            "order_user_id"=>$data['order_user_id'],//订单的购买者ID
            "role"=>$data['role'],//获得者身份
            "money"=>$data['money'],//获得金额
            "rate"=>$data['rate'],//金额比例
            "order_money"=>$data['order_money'],//订单总金额
            "type"=>$data['type'],//类型
        ];
        $this->save($data);

    }

    public function getRoleList()
    {
        return $this->rolelist;
    }

    public function getRoleTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['role']) ? $data['role'] : '');
        $list = $this->getRoleList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getUserNameAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['user_id']) ? $data['user_id'] : '');
        return User::where("id","=",$value)->value("nickname")??'';
    }
    public function getOrderUserNameAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_user_id']) ? $data['order_user_id'] : '');
        return User::where("id","=",$value)->value("nickname")??'';
    }
    public function getOrderNoAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['order_id']) ? $data['order_id'] : '');
        return Order::where("id","=",$value)->value("order_no")??'';
    }


}
